﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyAlbum.Utility;
using MyAlbum.DataAccess.LinqToSql;


namespace MyAlbum.Data.Linq
{
    public class AlbumRepository : IAlbumRepository
    {
        private string _connectionString;
        public AlbumRepository(string connectionString)
        {
            Assert.IsNotNull(connectionString);
            this._connectionString = connectionString;
        }

        public List<MyAlbum.Model.Album> GetItems()
        {
            return GetItems(true);
        }
        public List<MyAlbum.Model.Album> GetItems(bool visible)
        {
            using (var DB = new DataClasses1DataContext(this._connectionString))
            {
                return DB.Albums.Where(a => visible ? a.Visible : true).Select(s => new MyAlbum.Model.Album()
                {
                    Id = s.Id.ToString(),
                    Name = s.Name,
                    ParentId = s.ParentId.ToString(),
                    Timestamp = s.Timestamp,
                    Visible = s.Visible,
                    Url = s.Url,
                    Description = s.Description
                }).ToList();

            }
        }
        public void AddItem(MyAlbum.Model.Album album)
        {
            Assert.IsNotNull(album);
            Assert.IsNotNull(album.Name);
            Album a = new Album()
            {
                 Id=Guid.NewGuid(),
                Visible = album.Visible,
                Timestamp = album.Timestamp,
                ParentId =new Guid( album.ParentId),
                Name = album.Name,
                Url = album.Url,
                Description = album.Description
            };
            using (var DB = new DataClasses1DataContext(this._connectionString))
            {
                DB.Albums.InsertOnSubmit(a);
                DB.SubmitChanges();
            }
            album.Id = a.Id.ToString();
        }

        public void UpdateItem(MyAlbum.Model.Album album)
        {
            Assert.IsNotNull(album);
            Assert.IsNotNull(album.Name);
            using (var DB = new DataClasses1DataContext(this._connectionString))
            {
                Album old = DB.Albums.FirstOrDefault(a => a.Id.ToString() == album.Id);
                if (old != null)
                {
                    old.Name = album.Name;
                    old.Description = album.Description;
                    old.ParentId =new Guid( album.ParentId);
                    old.Url = album.Url;
                    old.Visible = album.Visible;
                    DB.SubmitChanges();
                }
            }
        }

        public void DeleteItem(MyAlbum.Model.Album album)
        {
            Assert.IsNotNull(album);
            using (var DB = new DataClasses1DataContext(this._connectionString))
            {
                Album old = DB.Albums.FirstOrDefault(a => a.Id.ToString() == album.Id);
                if (old != null)
                {
                    DB.Albums.DeleteOnSubmit(old);
                    DB.SubmitChanges();
                }
            }
        }


        public MyAlbum.Model.Album GetItemById(string albumId)
        {
            using (var DB = new DataClasses1DataContext(this._connectionString))
            {
                return (from a in DB.Albums
                        where a.Id.ToString() == albumId
                        select new MyAlbum.Model.Album()
                        {
                            Description = a.Description,
                            Id = a.Id.ToString(),
                            Name = a.Name,
                            ParentId = a.ParentId.ToString(),
                            Timestamp = a.Timestamp,
                            Url = a.Url,
                            Visible = a.Visible
                        }).SingleOrDefault();

            }
        }

        public List<MyAlbum.Model.Album> GetAlbumsBySectionId(string parentId, bool visible)
        {
            using (var DB = new DataClasses1DataContext(this._connectionString))
            {
                return DB.Albums.Where(a => a.ParentId ==new Guid( parentId) && (visible ? a.Visible : true)).Select(s => new MyAlbum.Model.Album()
                {
                    Id = s.Id.ToString(),
                    Name = s.Name,
                    ParentId = s.ParentId.ToString(),
                    Timestamp = s.Timestamp,
                    Visible = s.Visible,
                    Url = s.Url,
                    Description = s.Description
                }).ToList();
            }
        }
        

        public int CountItems(bool visible)
        {
            using (var DB = new DataClasses1DataContext(this._connectionString))
            {
                return DB.Albums.Count();
            }
        }

       
    }
}
